<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>访问本地文件、文件夹</title>
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <style>
        .btn {
            padding: 4px 8px;
            font-size: 14px;
            cursor: pointer;
            background-color: #eee;
            border: 1px solid #333;
            border-radius: 4px;
        }

        .btn:hover {
            background-color: #dddddd;
        }
    </style>
</head>

<body>
    <h3>api方式</h3>
    <button>选择文件夹</button>
    <hr>
    <h3>input方式</h3>
    <form action="">
        <label for="file">
            <input id="file" type="file" style="display: none">
            <span class="btn">选择文件</span>
        </label>
        <br>
        <br>
        <label for="directory">
            <input id="directory" type="file" style="display: none" webkitdirectory multiple>
            <span class="btn">选择文件夹</span>
        </label>
    </form>
    <pre>
        <code class="language-javascript">
            console.log(666)
        </code>
    </pre>

    <script>
        hljs.highlightAll();
        const btn = document.querySelector('button');
        const pre = document.querySelector('.language-javascript')
        btn.onclick = async function () {
            try {
                const handle = await showDirectoryPicker()//选择文件夹
                // showOpenFilePicker()//选择文件
                const root = await processHandle(handle)
                // 获取文件
                const fileHandle = root.children[0]
                const file = await fileHandle.getFile();
                const reader = new FileReader();
                reader.onload = e => {
                    console.log(e.target.result)
                    pre.innerHTML = e.target.result;
                    hljs.highlightAll();
                }
                reader.readAsText(file, 'utf-8')
            } catch {
                // 用户拒绝
            }
        }
        async function processHandle (handle) {
            if (handle.kind === 'file') {
                return handle;
            }
            handle.children = []
            // 得到异步迭代器
            const iter = handle.entries();
            for await (const item of iter) {
                handle.children.push(await processHandle(item[1]))
            }
            return handle;
        }


        const fileEl = document.getElementById('file')
        fileEl.onchange = async function (e) {
            // console.log(e.target.files,this.files)
            console.log(this.files[0].type)
            const reader = new FileReader();
            reader.readAsText(this.files[0], 'utf-8')
            reader.onload = e => {
                // console.log(e.target.result,reader.result)
                pre.innerHTML = reader.result;
                hljs.highlightAll();
            }
        }
        const directoryEl= document.getElementById('directory')
            directoryEl.onchange = async function (e) {
                console.log(e)
                console.log(this.files[0].type)
                const reader = new FileReader();
                reader.readAsText(this.files[0], 'utf-8')
                reader.onload = e => {
                    // console.log(e.target.result,reader.result)
                    pre.innerHTML = reader.result;
                    hljs.highlightAll();
                }
            }
    </script>
</body>

</html>